﻿/*
 * marqdown
 * --------
 * a C# Markdown processor
 * 
 * Copyright (c) 2010 Andrew Wall
 * http://code.google.com/p/marqdown/
 * 
 * 
 */


#region Copyright and license

/*

Copyright (c) 2010 Andrew Wall

 http://www.opensource.org/licenses/mit-license.php
  
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

#endregion


namespace Marqdown
{
    namespace StateMachine
    {
        public abstract class OrderedListStrategyBase
        {
            public void process(Info info, char c)
            {
                switch (c)
                {
                    case '.':
                        ChangeStateToDot(info);
                        break;
                    default:
                        // Ignore digits
                        if (!info.isDigit(c))
                        {
                            ResendText(info);
                        }
                        break;
                }
            }
            public abstract void ResendText(Info info);
            public abstract void ChangeStateToDot(Info info);
        }
        public class OrderedListStrategy : OrderedListStrategyBase
        {
            public override void ChangeStateToDot(Info info)
            {
                info.ChangeState(OrderedList.olDot);
            }
            public override void ResendText(Info info)
            {
                info.competitorFailed();
            }
        }

        public class OrderedList : CompetitiveState
        {
            internal static ConcreteState olDot = new OLDot();
            internal static ConcreteState olSpace = new OLSpace();
            internal static ConcreteState orderedListEOL = new OrderedListEOL();
            internal static ConcreteState olNewLine = new OrderedListNewLine();
            internal static ConcreteState postOrderedList = new PostOrderedList();
            internal static ConcreteState olDigit = new OLNewLine.OrderedListDigit();
            internal static ConcreteState looseDigit = new OLLoose.OrderedListDigit();
            private static OrderedListStrategy strategy = new OrderedListStrategy();
            public override void process(Info info, char c)
            {
                strategy.process(info, c);
            }

            public override void GetAction(ActionSetting a)
            {
                a.leading = "0123456789";
                a.action = Info.nop;
            }
        }
        public class OLDot : CommonState
        {
            public override void process(Info info, char c)
            {
                switch (c)
                {
                    case ' ':
                        NewTag(info, "ol");
                        //info.tag.SetClosingPrefix(info.prefix);
                        if (info.isNestedList())
                        {
                            info.tag.SetAppendage("");
                        }
                        // Indicate this is a 'tight' list
                        info.tag.Append('\n');
                        info.tag.NewLine();
                        info.ChangeState(OrderedList.olSpace);
                        break;
                    default:
                        //info.ResendAllAsPlain();
                        info.competitorFailed();
                        break;
                }
            }
        }
        public class OLSpace : ConcreteState
        {
            public override void process(Info info, char c)
            {
                switch (c)
                {
                    case ' ':
                        // Absorb spaces
                        break;
                    default:
                        info.NewChildTag();
                        info.tag.SetTagName("li");

                        info.ChangeState(OrderedList.orderedListEOL);
                        info.process(c);
                        break;
                }
            }
        }
        public class OrderedListEOL : ConcreteState
        {
            public override void process(Info info, char c)
            {
                switch (c)
                {
                    case '\n':
                        info.ChangeState(OrderedList.olNewLine);
                        break;
                    default:
                        info.tag.Append(c);
                        break;
                }
            }
        }
        public class OrderedListNewLine : ConcreteState
        {
            public override void process(Info info, char c)
            {
                switch (c)
                {
                    case '\n':
                        info.ChangeState(OrderedList.postOrderedList);
                        break;
                    case ' ':
                        info.tag.NewLine();
                        info.postNestingList = OrderedList.olNewLine;
                        info.ChangeState(UnorderedList.nestAbsorb);
                        break;
                    default:
                        if (info.isDigit(c))
                        {
                            info.ChangeState(OrderedList.olDigit);
                        }
                        else
                        {
                            info.tag.Append('\n');
                            info.ChangeState(OrderedList.orderedListEOL);
                            info.process(c);
                        }
                        break;
                }
            }
        }
        public class PostOrderedList : ConcreteState
        {
            public override void process(Info info, char c)
            {
                switch (c)
                {
                    case '\n':
                        // Absorb
                        break;
                    case ' ':
                        info.tag.NewLine();
                        // Make the list loose
                        info.setParentToLoose();
                        info.postNestingList = OrderedList.olNewLine;
                        info.ChangeState(UnorderedList.nestAbsorb);
                        break;
                    default:
                        if (info.isDigit(c))
                        {
                            info.ChangeState(OrderedList.looseDigit);
                        }
                        else
                        {
                            info.competitorFailed();
                            //info.ResendAllAsPlain();
                        }
                        break;
                }
            }
            public override void terminate(Info info)
            {
                info.tag.NewLine();
            }
        }

        namespace OLNewLine
        {
            public class OrderedListStrategy : OrderedListStrategyBase
            {
                public override void ChangeStateToDot(Info info)
                {
                    info.ChangeState(OrderedListDigit.olDot);
                }
                public override void ResendText(Info info)
                {
                    info.ChangeState(OrderedList.orderedListEOL);
                    info.ResendAll();
                }
            }


            public class OrderedListDigit : ConcreteState
            {
                internal static ConcreteState olDot = new OLDot();

                private static OrderedListStrategy strategy = new OrderedListStrategy();
                public override void process(Info info, char c)
                {
                    strategy.process(info, c);
                }
            }
            public class OLDot : ConcreteState
            {
                public override void process(Info info, char c)
                {
                    switch (c)
                    {
                        case ' ':
                            if (!info.tag.IsNested || !info.isParentLoose())
                            {
                                info.tag.NewLine();
                            }
                            info.PopChildTag();
                            info.ChangeState(OrderedList.olSpace);
                            break;
                        default:
                            info.ChangeState(OrderedList.orderedListEOL);
                            info.ResendAll();
                            break;
                    }
                }
            }

        }
        namespace OLLoose
        {
            public class OrderedListStrategy : OrderedListStrategyBase
            {
                public override void ChangeStateToDot(Info info)
                {
                    info.ChangeState(OrderedListDigit.olDot);
                }
                public override void ResendText(Info info)
                {
                    info.competitorFailed();
                }
            }
            public class OrderedListDigit : ConcreteState
            {
                internal static ConcreteState olDot = new OLDot();
                internal static ConcreteState olSpace = new OLSpace();
                private static OrderedListStrategy strategy = new OrderedListStrategy();
                public override void process(Info info, char c)
                {
                    strategy.process(info, c);
                }
            }
            public class OLDot : ConcreteState
            {
                public override void process(Info info, char c)
                {
                    switch (c)
                    {
                        case ' ':
                            info.tag.NewLine();
                            info.PopChildTag();
                            info.ChangeState(OrderedListDigit.olSpace);
                            break;
                        default:
                            info.competitorFailed();
                            //info.ResendAllAsPlain();
                            break;
                    }
                }
            }
            public class OLSpace : ConcreteState
            {
                public override void process(Info info, char c)
                {
                    switch (c)
                    {
                        case ' ':
                            // Absorb spaces
                            break;
                        default:
                            info.NewChildTag();
                            info.tag.SetTagName("li");
                            info.setParentToLoose();

                            info.ChangeState(OrderedList.orderedListEOL);
                            info.process(c);
                            break;
                    }
                }
            }

        }
    }
}
